package com.daxue.configtest.service.impl;

import com.daxue.configtest.base.QueryConditions;
import com.daxue.configtest.domian.RemoteConfig;
import com.daxue.configtest.exception.impl.BadArgumentException;
import com.daxue.configtest.exception.impl.ConflictException;
import com.daxue.configtest.exception.impl.NotFoundException;
import com.daxue.configtest.repository.remoteconfig.ConfigEntity;
import com.daxue.configtest.repository.remoteconfig.ConfigRepository;
import com.daxue.configtest.service.RemoteConfigService;
import com.daxue.configtest.utils.QueryParser;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import static com.daxue.configtest.repository.remoteconfig.ConfigEntity.convert;

/**
 * @author daxue0929
 * @date 2023/3/25
 */

@AllArgsConstructor
public class RemoteConfigServiceImpl implements RemoteConfigService {


    private final ConfigRepository remoteConfigRepository;

    @Override
    public RemoteConfig create(final RemoteConfig config) throws ConflictException, BadArgumentException {
        return remoteConfigRepository.create(convert(config)).convert();
    }

    @Override
    public RemoteConfig read(final String id) throws NotFoundException, ConflictException {
        return remoteConfigRepository.read(id).convert();
    }

    @Override
    public RemoteConfig update(
        final String id, final RemoteConfig config
    ) throws ConflictException, BadArgumentException, NotFoundException {
        return remoteConfigRepository.update(id, convert(config)).convert();
    }

    @Override
    public RemoteConfig delete(final String id) throws NotFoundException, ConflictException {
        return remoteConfigRepository.delete(id).convert();
    }

    @Override
    public RemoteConfig readOne(final String query) throws BadArgumentException, NotFoundException, ConflictException {
        QueryConditions queryConditions = QueryParser.parse(query);
        ConfigEntity configEntity = remoteConfigRepository.readOne(queryConditions);
        return configEntity.convert();

    }

    @Override
    public Page<RemoteConfig> readAll(
        final Pageable pageable, final String query
    ) throws BadArgumentException, ConflictException {
        return remoteConfigRepository.readAll(pageable, QueryParser.parse(query))
            .map(ConfigEntity::convert);
    }

//    @Override
//    public List<String> readAllUniqueField(
//        final String fieldName, final String query
//    ) throws BadArgumentException, ConflictException {
//        return remoteConfigRepository.readAllUniqueFields(field(fieldName), QueryParser.parse(query));
//    }
}
